package com.sendpo.zjx.sendpo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.PullableRecyclerView;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.JobDomain;
import com.sendpo.zjx.sendpo.domain.JobRegisterDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zjx on 2018
 */

public class YinPinZheActivity extends AppCompatActivity {
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.index_head)
    RelativeLayout indexHead;
    @BindView(R.id.recyclerview)
    PullableRecyclerView recyclerview;
    @BindView(R.id.pulltofresh)
    PullToRefreshLayout pulltofresh;

    private RecylcerAdaper adpter;
    public int jobid = 0;
    private LinearLayoutManager lm;
    private int pagenow = 1;
    private int pagesize = 10;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yinpinzhelist);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        this.jobid = getIntent().getIntExtra("jobid", 0);
        System.out.println("*********jobid:" + this.jobid);
        this.lm = new LinearLayoutManager(this, 1, false);
        this.recyclerview.setLayoutManager(this.lm);
        List<JobRegisterDomain.DataBean> dataBeanList = new ArrayList<>();
        recyclerview.setAdapter(adpter = new RecylcerAdaper(this,dataBeanList));
        this.pulltofresh.setPullDownEnable(true);
        this.pulltofresh.setPullUpEnable(true);
        this.pulltofresh.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
            public void onLoadMore(PullToRefreshLayout paramAnonymousPullToRefreshLayout) {
               pagenow++;
               append(pagenow);;
            }

            public void onRefresh(PullToRefreshLayout paramAnonymousPullToRefreshLayout) {
                pagenow=1;
                query(pagenow);;
            }
        });
        this.pulltofresh.autoRefresh();
    }

    @OnClick(value = {R.id.backbt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
        }
    }
    public class RecylcerAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        public List<JobRegisterDomain.DataBean> dataBeen;
        public LayoutInflater inflater;
        public RecylcerAdaper(Context context, List<JobRegisterDomain.DataBean> dataBeans){
            this.dataBeen=dataBeans;
            inflater=LayoutInflater.from(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new IndexHoler(this.inflater.inflate(R.layout.layout_yinpinzhelist_listitem,parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                  IndexHoler ih= (IndexHoler) holder;
            final JobRegisterDomain.DataBean dataBean = (JobRegisterDomain.DataBean)this.dataBeen.get(position);
            ih.realname.setText("" + dataBean.getJobName());
            ih.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("data", dataBean);
               Tools.redirectActivity(YinPinZheActivity.this, ZhaoPinShowUserActivity.class, map);
                }
            });
            ih.yinpinzhe_bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("data", dataBean);
                    Tools.redirectActivity(YinPinZheActivity.this, ZhaoPinShowUserActivity.class, map);
                }
            });
        }

        @Override
        public int getItemCount() {
            return dataBeen.size();
        }

        public void delete(int paramInt)
        {
            this.dataBeen.remove(paramInt);
            notifyDataSetChanged();
        }

        public void deleteRow(int paramInt)
        {
            this.dataBeen.remove(paramInt);
            notifyDataSetChanged();
        }

        public void initAppend(List<JobRegisterDomain.DataBean> paramList)
        {
            this.dataBeen.addAll(paramList);
            notifyDataSetChanged();
        }

        public void initRefresh(List<JobRegisterDomain.DataBean> paramList)
        {
            this.dataBeen.removeAll(this.dataBeen);
            this.dataBeen.addAll(paramList);
            notifyDataSetChanged();
        }
        public class IndexHoler
                extends RecyclerView.ViewHolder
        {
            public TextView realname;
            public View rootView;
            public Button yinpinzhe_bt;

            public IndexHoler(View paramView)
            {
                  super(paramView);
                this.rootView = paramView;
                this.yinpinzhe_bt = paramView.findViewById(R.id.yinpinzhe_bt);
                this.realname = paramView.findViewById(R.id.realname);
            }
        }
    }

    public void query(int pagenow) {
        List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pagenow", pagenow + "");
        map.put("jobId", this.jobid  + "");
        Xutils.POST(HttpUrl.userregisterjobqueryuserbyjobid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("********************"+result);
                pulltofresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                Gson gson = new Gson();
                JobRegisterDomain domain = gson.fromJson(result, JobRegisterDomain.class);
                switch (domain.getState()) {
                    case HttpCode.SUCCESS:
                        if (domain.getData() != null && domain.getData().size() != 0) {
                            if (adpter == null) {
                                recyclerview.setAdapter(adpter=new RecylcerAdaper(YinPinZheActivity.this,domain.getData()));
                            } else {
                                adpter.initRefresh(domain.getData());
                            }

                        }
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                pulltofresh.refreshFinish(PullToRefreshLayout.FAIL);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void append(int pagenow) {
        List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pagenow", pagenow + "");
        map.put("jobId", this.jobid  + "");
        Xutils.POST(HttpUrl.userregisterjobqueryuserbyjobid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("********************"+result);
                pulltofresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                Gson gson = new Gson();
                JobRegisterDomain domain = gson.fromJson(result, JobRegisterDomain.class);
                switch (domain.getState()) {
                    case HttpCode.SUCCESS:
                        if (domain.getData() != null && domain.getData().size() != 0) {
                            if (adpter == null) {
                                recyclerview.setAdapter(adpter=new RecylcerAdaper(YinPinZheActivity.this,domain.getData()));
                            } else {
                                adpter.initAppend(domain.getData());
                            }

                        }
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                pulltofresh.refreshFinish(PullToRefreshLayout.FAIL);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
